﻿using System;
using System.Web.Mvc;
using Data;
using Models;

namespace Web.Controllers
{
    public class UsersController : BaseController
   {
       //
       // GET: /Users/

      [HttpGet]
      public ActionResult Index(int? id,int? page)
      {
        if (id != null)
        {
            var dbUsers = new UsersData();
            var usersList = dbUsers.GetUsers(id:Convert.ToInt32(id));
            if(usersList != null)
            {
                var user = usersList[0];
                var dbNews = new NewsData();
                ViewBag.user = user;
                var newsList = dbNews.GetNews(author:user.Login);

                if (newsList != null)
                {
                    var pager = new Pager(newsList.Count, Request.Url.LocalPath, page);
                    if (pager.Check() == null)
                    {
                        pager.CurrentPage = 1;
                    }
                    ViewBag.pager = pager;
                    

                    return View(newsList);
                }
                return View();
            }
            
               ViewBag.Message = "User does not exist";
               return View();
         }
         return Redirect("/News");
      }

   }
}
